from queue import Queue

class Lift(object):
    """defines a Lift"""
    def __init__(self, liftcapacity):
        self.current = 0
        self.capacity = liftcapacity
        self.people = []
        # possible cycle states:
        # 0 = down entering; 1 = driving up; 2 = up dismounting; 3 = drving empty
        self.cycle = 3

    def dequeueperson(self, person):
        try:
            self.people.remove(person)
        except ValueError:
            print(str(person.id) + " not found in queue")

    # adds people to the queue
    def addperson(self, person):
        self.people.append(person)
        person.state = 2
        print("{0} waiting (lift)".format(person.id))

    # handles a timestep for all people queued
    def handle(self):
        # check every person
        for person in self.people:
            if self.cycle == 0 and person.state == 2:
                if not self.current + 1 > self.capacity:
                    person.state = 3
                    self.current += 1
                    print("{0} entering (lift: {1})".format(person.id, self.cycle))
            elif self.cycle == 2 and person.state == 3:
                person.state = 4
                self.current -= 1
                print("{0} leaving (lift: {1})".format(person.id, self.cycle))
            elif person.state == 4:
                self.dequeueperson(person)
                person.state = 5

        # let the lift cycle
        if not self.cycle == 3:
            self.cycle += 1
        else:
            self.cycle = 0
        print("lift has cycled (now {0})".format(self.cycle))
